package com.yc.tomcat.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ParseXml {
	private static Map<String, String> map = new HashMap<String, String>();
	public ParseXml() {
		init();
	}
	/**
	 * 解析xml文件
	 */
	@SuppressWarnings("unchecked")

	private void init() {
		SAXReader reader = new SAXReader();
		Document doc = null;
		try(InputStream is = this.getClass().getClassLoader().getResourceAsStream("web.xml")){
			doc = reader.read(is);
			List<Element> mines = doc.selectNodes("//mime-mapping");
			for(Element el : mines) {
				map.put(el.selectSingleNode("extension").getText().trim(), el.selectSingleNode("mime-type").getText().trim());
			}
//			map.forEach((key, val) ->{
//				System.out.println(key + " : " + val);
//			});
		} catch (IOException  e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	public Map<String, String> getMap(){
		return map;
	}
	public static String getContentType(String key) {
		return map.getOrDefault(key, null);	
	}
}
